from logging import exception
from time import sleep, strftime,localtime,time
from selenium.webdriver.common.by import By
from common.Utils import ForumDriver
from selenium.common.exceptions import NoSuchFrameException
scrpath = "C:\\ForumTest\\Screenshot"
class ForumEdit:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver
        url = "http://127.0.0.1:58080/index.html"
        self.driver.get(url)

    def EditUI(self):
        # self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # sleep(2)
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #测试发布帖子的UI文本存在和选择功能
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        sleep(2)
        actal = self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(1) > label > strong").text
        assert actal == "版块"
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_borad")
        act = self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(2) > label > strong").text
        assert act == "标题"
        self.driver.find_element(By.CSS_SELECTOR,"#article_post_title")
        for i in range(1,10):
            self.driver.find_element(By.CSS_SELECTOR,f"#article_post_borad > option:nth-child({i})").click()
            sleep(2)
        #截图保存
        file_name = strftime("%Y-%m-%d %H-%M-%S EditUI", localtime()) + ".png"
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)

    def EditFunc(self):
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys("自动化测试")

        self.driver.find_element(By.CSS_SELECTOR, "#edit-article > div.editormd-toolbar > div > ul > li:nth-child(4) > a > i").click()
        sleep(2)

        self.driver.find_element(By.CSS_SELECTOR,"#article_post_submit").click()
#
# T = ForumEdit()
# sleep(2)
# T.ForumEditUI()
# sleep(2)
# T.ForumEditFunc()